package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Employee;
import com.ssh.entity.Position;
import com.ssh.entity.Product;
import com.ssh.manager.EmployeeManager;

@Service
public class EmployeeManagerImpl implements EmployeeManager {
	@Resource
	private IDao daoImpl;
	
	@Override
	public List<Employee> getAllByEid(Long eid) {
		String sql = "from Employee where del!=1 and employee.id=? order by createDate desc";
		List<Employee> employees = (List<Employee>)daoImpl.findListBySql(sql, new Object[]{eid}, false, true, false, 0, -1);
		return employees;
	}

	@Override
	public List<Employee> getAllByPid(Long pid) {
		String sql = "from Employee where del!=1 and position.id=? order by createDate desc";
		List<Employee> employees = (List<Employee>)daoImpl.findListBySql(sql, new Object[]{pid}, false, true, false, 0, -1);
		return employees;
	}

	@Override
	public Employee saveUpdate(Employee employee) {
		return daoImpl.saveOrUpdateDomain(employee);
	}

	@Override
	public Employee getById(Long id) {
		return daoImpl.getDomain(Employee.class, id);
	}

	@Override
	public List<Employee> getAll() {
		String sql = "from Employee where del!=1 order by createDate desc";
		List<Employee> employees = (List<Employee>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return employees;
	}

	@Override
	public void findListByPage(Map conditionMap, Page page, String isDesc) {
		try {
			String sql = "select count(*) from Employee where del!=1";
			String findSql = "from Employee where del!=1";
			if(conditionMap.get("position")!=null && !conditionMap.get("position").equals("")){
				sql += " and position.id="+conditionMap.get("position");
				findSql += " and position.id="+conditionMap.get("position");
			}
			if(conditionMap.get("pid")!=null && !conditionMap.get("pid").equals("")){
				sql += " and employee.userName like '%"+conditionMap.get("pid")+"%'";
				findSql += " and employee.userName like '%"+conditionMap.get("pid")+"%'";
			}
			int sum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(sum));
			page.countTotalPageNum();
			
			findSql += " order by createDate "+isDesc;
			List<Employee> employees = (List<Employee>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
			page.setPageData(employees);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Employee login(String userName) {
		List<Employee> employees = (List<Employee>)daoImpl.findListBySql("from Employee where userName=?", new Object[]{userName}, false, true, false, 0, 1);
	 if (employees.size()>0) {
		return employees.get(0);			
	 }
	 return null;
	}

}
